
/*
 * Copyright (C) Roman Arutyunyan
 */


#include <ngx_config.h>
#include <ngx_core.h>
#include "ngx_rtmp_bandwidth.h"


void
ngx_rtmp_update_bandwidth(ngx_rtmp_bandwidth_t *bw, uint32_t bytes)
{
    if (ngx_cached_time->sec > bw->intl_end) {
        bw->bandwidth = (bw->intl_bytes / NGX_RTMP_BANDWIDTH_INTERVAL / 2) >> 7; 
        bw->intl_bytes = 0;
        bw->intl_end = ngx_cached_time->sec + NGX_RTMP_BANDWIDTH_INTERVAL;
    }

    bw->bytes += bytes;
    bw->intl_bytes += bytes;
}

void ngx_rtmp_update_fps(ngx_rtmp_bandwidth_t *bw, uint32_t frames)
{
    if (ngx_cached_time->sec > bw->intl_end_fps) {
        bw->fps = bw->nb_frames / NGX_RTMP_BANDWIDTH_INTERVAL / 2;
        bw->nb_frames =  0;
        bw->intl_end_fps = ngx_cached_time->sec + NGX_RTMP_BANDWIDTH_INTERVAL;
    }
    bw->nb_frames += frames;
}

void ngx_rtmp_update_delay(ngx_rtmp_bandwidth_t *bw, int32_t delay)
{
    bw->delay_ms = delay;
}
